//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.zr.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.stream.Stream;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

public class DateUtils {
    public static final String BCD_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String SSS_YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
    public static final String MM_DD = "MM-dd";
    public static final String YYYYMMDD = "yyyy-MM-dd";
    public static final String YYMMDD = "yy-MM-dd";
    public static final String YYYYMMDD_POINT = "yyyy.MM.dd";
    public static final String YYYYMM = "yyyyMM";
    public static final String YYYYWW = "yyyyww";
    public static final String YYYYMMDDHH = "yyyy-MM-dd HH";
    public static final String YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
    public static final String YYYYMMDD000000 = "yyyy-MM-dd 00:00:00";
    public static final String YYYYMMDDHH0000 = "yyyy-MM-dd HH:00:00";
    public static final String YYYYMMDDHHMM00 = "yyyy-MM-dd HH:mm:00";
    public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String CHN_YMD = "yyyy年MM月dd日";
    public static final String CHN_YMD_HH = "yyyy-MM-dd HH时";
    public static final String CHN_YMD_HHMM = "yyyy年MM月dd日HH:mm";
    public static final String CHN_MDHHMM = "MM月dd日HH时mm分";
    public static final String CHN_MD = "MM月dd日";
    public static final String HHMM = "HH:mm";
    public static final String YYYYMMD235959 = "yyyy-MM-dd 23:59:59";
    public static final String YYYYMMDDSTR = "yyyyMMdd";
    public static final String DAY = "MMdd";
    public static final String YEAR = "yyyy";
    public static final String FILE_TIME = "yyyy年MM月dd日HH时mm分";
    public static final String FILE_DATE = "yyyyMMddHHmm";
    public static final String YYYY = "yyyy";
    public static final String MM = "MM";
    public static final String WW = "WW";
    public static final String DD = "dd";
    public static final String HH = "HH";
    public static final String MMF = "mm";
    public static final String SS = "ss";
    public static final String GMT_8 = "GMT+8";
    public static final String MIN_TIME = "00:00:00";
    public static final String MAX_TIME = "23:59:59";

    public DateUtils() {
    }

    public static String format(Date date, String pattern) {
        if (date != null && !StringUtils.isBlank(pattern)) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.SIMPLIFIED_CHINESE);
            return sdf.format(date);
        } else {
            return "";
        }
    }

    public static String format(Calendar calendar, String pattern) {
        if (StringUtils.isBlank(pattern)) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(calendar.getTime());
        }
    }

    public static Date parse2Date(String datestring, String pattern) {
        if (!StringUtils.isBlank(pattern) && !StringUtils.isBlank(datestring)) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);

            try {
                return sdf.parse(datestring);
            } catch (ParseException var4) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static Date parse2PayTime(String datestring, String pattern) throws Exception {
        if (!StringUtils.isBlank(pattern) && !StringUtils.isBlank(datestring)) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(datestring);
        } else {
            return null;
        }
    }

    public static Calendar parse2Calendar(String datestring, String pattern) {
        if (!StringUtils.isBlank(pattern) && !StringUtils.isBlank(datestring)) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);

            try {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(sdf.parse(datestring));
                return calendar;
            } catch (ParseException var4) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(5, 1);
        Date firstDayOfMonth = calendar.getTime();
        return firstDayOfMonth;
    }

    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(5, 1);
        calendar.add(2, 1);
        calendar.add(5, -1);
        Date lastDayOfMonth = calendar.getTime();
        return lastDayOfMonth;
    }

    public static Integer getBackYear(Integer num) {
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(1);
        return year - num;
    }

    public static Date getAgoYear(Integer num) {
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(1);
        Integer month = calendar.get(2);
        int day = calendar.get(5);
        calendar.set(year + num, month, day);
        return calendar.getTime();
    }

    public static Date getAgoMonth(Integer num) {
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(1);
        Integer month = calendar.get(2);
        int day = calendar.get(5);
        calendar.set(year, month + num, day);
        return calendar.getTime();
    }

    public static Date getAgoDayByDate(Date date, Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(6, num);
        return calendar.getTime();
    }

    public static String getAgoDayByDate(String date, Integer num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;

        try {
            parse = sdf.parse(date);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse);
        calendar.add(6, num);
        return sdf.format(calendar.getTime());
    }

    public static String getYearMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return sdf.format(calendar.getTime());
    }

    public static Date getAgoHoursByDate(Date date, Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(12, num);
        return calendar.getTime();
    }

    public static void main(String[] args) {
        Long ms = null;
        long mins = ms / 60000L;
        System.out.println(mins + "min");
    }

    public static Date getBackEndYear(Integer num) {
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(1);
        calendar.set(year - num, 11, 31);
        return calendar.getTime();
    }

    public static Date getBackStartYear(Integer num) {
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(1);
        calendar.set(year - num, 0, 1);
        return calendar.getTime();
    }

    public static String getYear() {
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(1);
        return String.valueOf(year);
    }

    public static String getBackDay(Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(6, -num);
        Date time = calendar.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static Date getAgoDay(Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(6, num);
        return calendar.getTime();
    }

    public static Date dateToBeginOfDay(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(11, 0);
            calendar.set(12, 0);
            calendar.set(13, 0);
            calendar.set(14, 0);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public static Date dateToEndOfDay(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(11, 23);
            calendar.set(12, 59);
            calendar.set(13, 59);
            calendar.set(14, 0);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public static Long convert2Day(Long mm) {
        if (null == mm) {
            return 0L;
        } else {
            Long day = mm / 86400L;
            return day;
        }
    }

    public static Long convert2mill(Long day) {
        return null == day ? 0L : day * 24L * 60L * 60L;
    }

    public static int compareDate(String start, Date end) {
        if (!StringUtils.isBlank(start) && end != null) {
            String endStr = format(end, "yyyy-MM-dd");
            Date startDate = parse2Date(start, "yyyy-MM-dd");
            Date endDate = parse2Date(endStr, "yyyy-MM-dd");
            return startDate != null && endDate != null ? startDate.compareTo(endDate) : -1;
        } else {
            return -1;
        }
    }

    public static Boolean compareDate(String start, String end) {
        if (!StringUtils.isBlank(start) && !StringUtils.isBlank(end)) {
            Date startDate = parse2Date(start, "yyyy-MM-dd");
            Date endDate = parse2Date(end, "yyyy-MM-dd");
            return startDate != null && endDate != null ? startDate.compareTo(endDate) >= 0 : false;
        } else {
            return false;
        }
    }

    public static Boolean compareDate(String start, String end, String deteFormat) {
        if (!StringUtils.isBlank(start) && !StringUtils.isBlank(end)) {
            Date startDate = parse2Date(start, deteFormat);
            Date endDate = parse2Date(end, deteFormat);
            return startDate != null && endDate != null ? startDate.compareTo(endDate) >= 0 : false;
        } else {
            return false;
        }
    }

    public static String getFirstDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.add(4, 0);
        cal.set(7, 2);
        Date time = cal.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getLastDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.set(7, cal.getActualMaximum(7));
        cal.add(7, 1);
        Date time = cal.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(2, 0);
        cal.set(5, 1);
        Date time = cal.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getLastDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(5, cal.getActualMaximum(5));
        Date time = cal.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getPrecedingMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(5, 0);
        Date time = calendar.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getStartOrEndDayOfQuarter(boolean isFirst) {
        LocalDate today = LocalDate.now();
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            today = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            today = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }

        return today.toString();
    }

    public static String getYearStart() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(6, 1);
        Date time = cal.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getYearEnd() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(1, 1);
        cal.set(6, -1);
        Date time = cal.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getYearLastDay() {
        Calendar cal = Calendar.getInstance();
        cal.set(2, cal.getActualMaximum(2));
        cal.set(5, cal.getActualMaximum(5));
        Date time = cal.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getLastYearLastDayEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(1, -1);
        calendar.set(6, calendar.getActualMaximum(6));
        Date time = calendar.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getAgoYear(String date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(parse2Date(date, "yyyy-MM-dd"));
        cal.add(1, -1);
        Date time = cal.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static String getAgoMonth(String date, Integer num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(parse2Date(date, "yyyy-MM-dd"));
        cal.add(2, -num);
        Date time = cal.getTime();
        return format(time, "yyyy-MM-dd");
    }

    public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap();
        Stream<Entry<K, V>> stream = map.entrySet().stream();
        stream.sorted(Entry.comparingByKey()).forEachOrdered((e) -> {
            result.put(e.getKey(), e.getValue());
        });
        return result;
    }

    public static long getPlaus(Calendar c) {
        c.set(5, c.get(5) + 1);
        return c.getTimeInMillis();
    }

    public static Boolean isBeforeNow(Date date, int forwardDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(6, forwardDay);
        return calendar.getTime().getTime() < System.currentTimeMillis();
    }

    public static Long betweenMilliseconds(Date startTime, Date endTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        long timeInMillis1 = calendar.getTimeInMillis();
        calendar.setTime(endTime);
        long timeInMillis2 = calendar.getTimeInMillis();
        return timeInMillis2 - timeInMillis1;
    }

    public static Long betweenDays(Date startTime, Date endTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        long timeInMillis1 = calendar.getTimeInMillis();
        calendar.setTime(endTime);
        long timeInMillis2 = calendar.getTimeInMillis();
        long betweenDays = (timeInMillis2 - timeInMillis1) / 86400000L;
        return betweenDays;
    }

    public static Long betweenDays(String startTime, String endTime) {
        if (!StringUtils.isBlank(startTime) && !StringUtils.isBlank(endTime)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            try {
                Date startDate = dateFormat.parse(startTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);
                long timeInMillis1 = calendar.getTimeInMillis();
                Date endDate = dateFormat.parse(endTime);
                calendar.setTime(endDate);
                long timeInMillis2 = calendar.getTimeInMillis();
                long betweenDays = (timeInMillis2 - timeInMillis1) / 86400000L;
                return betweenDays;
            } catch (ParseException var12) {
                var12.printStackTrace();
                return 0L;
            }
        } else {
            return 0L;
        }
    }

    public static boolean isLatestWeek(Date addtime, Date now) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(5, -7);
        Date before7days = calendar.getTime();
        return before7days.getTime() < addtime.getTime();
    }

    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        return date.after(begin) && date.before(end);
    }

    public static Date getAgoDayAppointTime(int num, String pattern) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        calendar.add(5, -1);
        Date time = calendar.getTime();
        fmt.format(calendar.getTime());
        return new Date();
    }

    public static List<String[]> spot2section(List<Date> dateList, String pattern) {
        if (CollectionUtils.isEmpty(dateList)) {
            return new ArrayList();
        } else {
            dateList.sort(Date::compareTo);
            List<String[]> res = new ArrayList();
            String[] arr = new String[]{format((Date)dateList.get(0), pattern), format((Date)dateList.get(0), pattern)};

            String format;
            for(Iterator var4 = dateList.iterator(); var4.hasNext(); arr[1] = format) {
                Date value = (Date)var4.next();
                format = format(value, pattern);
                String endTime = arr[1];
                Date date = parse2Date(endTime, pattern);
                if (betweenDays(date, value) > 1L) {
                    res.add(arr);
                    arr = new String[]{format, null};
                }
            }

            res.add(arr);
            return res;
        }
    }

    public static Date getToday() {
        LocalDate localDate = LocalDate.now();
        return Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
    }

    public static Date getNextDay(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
        LocalDate yesterday = localDate.plusDays(1L);
        return Date.from(yesterday.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
    }

    public static boolean isToday(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
        LocalDate today = LocalDate.now();
        return today.equals(localDate);
    }

    public static Date getAgoMonthByDate(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(2, num);
        return calendar.getTime();
    }

    public static Date getAgoYearByDate(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(1, num);
        return calendar.getTime();
    }

    public static Boolean judgeWhetherToDay(Date date) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
        LocalDateTime localTime = LocalDateTime.parse(format(date, "yyyy-MM-dd HH:mm:ss"), dtf);
        LocalDateTime startTime = LocalDate.now().atTime(0, 0, 0);
        LocalDateTime endTime = LocalDate.now().atTime(23, 59, 59);
        return localTime.isAfter(startTime) && localTime.isBefore(endTime) ? true : false;
    }

    public static Date getBeforeLastDate(Date date, Integer type) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (type == 1) {
            calendar.add(6, -1);
        } else if (type == 3) {
            boolean isFirstSunday = calendar.getFirstDayOfWeek() == 1;
            int weekDay = calendar.get(7);
            if (isFirstSunday) {
                --weekDay;
                if (weekDay == 0) {
                    weekDay = 7;
                }
            }

            calendar.add(6, -weekDay);
        } else {
            int i;
            if (type == 4) {
                i = calendar.get(5);
                calendar.add(6, -i);
            } else if (type == 5) {
                i = calendar.get(1);
                calendar.set(1, i - 1);
                calendar.set(2, 11);
                calendar.set(5, 31);
            }
        }

        return calendar.getTime();
    }

    public static Boolean judgeFirstDayMonth(Date date) {
        SimpleDateFormat sd = new SimpleDateFormat("dd");
        return Objects.equals("01", sd.format(date));
    }

    public static Boolean judgeFirstDayYear(Date date) {
        SimpleDateFormat smd = new SimpleDateFormat("MMdd");
        return Objects.equals("0101", smd.format(date));
    }

    public static Integer getCurrentWeek(Date date) {
        Date agoDayByDate = getAgoDayByDate(date, -7);
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(2);
        calendar.setTime(agoDayByDate);
        int week = calendar.get(3);
        int year = calendar.getWeekYear();
        String day = String.valueOf(year) + (week < 10 ? "0" + week : week);
        return Integer.valueOf(day);
    }

    public static Date getEarlyDate(Date date) {
        Date now = new Date();
        return date.compareTo(now) > 0 ? now : date;
    }

    public static boolean isThisTime(Date date, String pattern) {
        if (date == null) {
            return false;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            String param = sdf.format(date);
            String now = sdf.format(new Date());
            return param.equals(now);
        }
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        if (null == localDateTime) {
            return null;
        } else {
            ZoneId zoneId = ZoneId.systemDefault();
            ZonedDateTime zdt = localDateTime.atZone(zoneId);
            Date date = Date.from(zdt.toInstant());
            return date;
        }
    }

    public static String getTime(Long diff) {
        long hours = diff / 3600000L;
        Long hourSurplus = diff % 3600000L;
        if (hourSurplus == 0L) {
            return hours + ":00:00";
        } else {
            long minutes = (diff - hours * 3600000L) / 60000L;
            Long minSurplus = (diff - hours * 3600000L) % 60000L;
            if (minSurplus == 0L) {
                return hours + ":" + minutes + ":00";
            } else {
                long s = (diff - hours * 3600000L - minutes * 60000L) / 1000L;
                return hours + ":" + minutes + ":" + s;
            }
        }
    }

    public static String getChnTime(long diff) {
        long hours = diff / 3600000L;
        long hourSurplus = diff % 3600000L;
        if (hourSurplus == 0L) {
            return hours + "时00分00秒";
        } else {
            long minutes = (diff - hours * 3600000L) / 60000L;
            long minSurplus = (diff - hours * 3600000L) % 60000L;
            if (minSurplus == 0L) {
                return hours + "时" + minutes + "分00秒";
            } else {
                long s = (diff - hours * 3600000L - minutes * 60000L) / 1000L;
                return hours + "时" + minutes + "分" + s + "秒";
            }
        }
    }

    public static String getTimeMin(Long ms) {
        if (Objects.isNull(ms)) {
            return "0min";
        } else {
            long mins = ms / 60000L;
            return mins + "min";
        }
    }

    public static boolean isSameYear(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return cal1.get(1) == cal2.get(1);
    }

    public static String getCurrentQuarter() {
        Date currentDate = new Date();
        String currentMinuteStr = DateUtils.format(currentDate, DateUtils.MMF);
        Long currentMinute = Long.valueOf(currentMinuteStr);
        long QuarterNum = currentMinute / 15L + 1;
        String currentHour = DateUtils.format(currentDate, "yyyyMMddHH");
        return currentHour + "-" + QuarterNum;
    }
}
